package GUI;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Toolkit;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;

import javax.swing.JTextField;
import javax.swing.JButton;

//import sun.security.krb5.internal.PAEncTSEnc;
import Controller.DBcontroller;
import Entity.Prices;
import Entity.Complaint.validateErrs;
import Entity.Prices.pvalidateErrs;
import Entity.Request;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * this is the GUI for requeest for changing rates. the parkin lot manager will enter his request and send it to the company manager.
 * @author Moran Abilea
 *
 */
public class RequestForChangingRatesBoundary extends JFrame {

	private JPanel contentPane;
	private JTextField OccasionalFrom;
	private JTextField OccasionalTo;
	private JTextField OrderedFrom;
	private JTextField OrderedTo;
	JFrame frame;
	JPanel lastPanel;
	public void setLastPanel (JPanel panel)
	{
		this.lastPanel=panel;
	}
	public JPanel getLastPanel()
	{
		return (JPanel)lastPanel;
	}
	public void setFrame (JFrame frame)
	{
		this.frame=frame;
	}
	private Request myGUIr=new Request(DBcontroller.getCurrentParkingLotNum(),0.0,0.0); // just for having a new entity for request.
	private boolean okflag=false;

	/**
	 * is the constructor. it will show the manager the existing prices in his parking lot and he will enter a new one. it also has a send buttom to validate the changes.
	 */
	public RequestForChangingRatesBoundary() {
		setTitle("Request For Changing Rates Window");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 409);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		int currentNum=DBcontroller.getCurrentParkingLotNum(); // assuming that i know that the parking lot manager of X was logged in.
		Prices myp=DBcontroller.getCurrentPrices(currentNum); // getting the current prices for this paring lot.
		
		JLabel lblNewLabel = new JLabel("Parking Lot Rates - parking number "+currentNum);
		lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblNewLabel.setBounds(97, 11, 239, 22);
		contentPane.add(lblNewLabel);
		
		JLabel lblCurrentPrices = new JLabel("Current Prices:");
		lblCurrentPrices.setBounds(10, 38, 117, 14);
		contentPane.add(lblCurrentPrices);
		
		JLabel lblNewLabel_1 = new JLabel("Occasional Parking Place price: "+Double.toString(myp.getOccasionalParkingPlace()));
		lblNewLabel_1.setBounds(78, 53, 287, 14);
		contentPane.add(lblNewLabel_1);
		
		JLabel lblOrderedParkingPlace = new JLabel("Ordered Parking Place price: "+Double.toString(myp.getOrderedParkingPlace()));
		lblOrderedParkingPlace.setBounds(78, 72, 320, 14);
		contentPane.add(lblOrderedParkingPlace);
		
		JLabel lblMonthlyRegularSubscriber = new JLabel("Monthly Regular Subscriber price: "+Double.toString(myp.getMonthlyRegularSubscriber()));
		lblMonthlyRegularSubscriber.setBounds(78, 89, 320, 14);
		contentPane.add(lblMonthlyRegularSubscriber);
		
		JLabel lblFullMonthlySubscriber = new JLabel("Full Monthly Subscriber price: "+Double.toString(myp.getFullMonthlySubscriber()));
		lblFullMonthlySubscriber.setBounds(78, 107, 254, 14);
		contentPane.add(lblFullMonthlySubscriber);
		
		JLabel lblRegularSubscriberFor = new JLabel("Regular Subscriber For Many Cars price:"+Double.toString(myp.getRegularSubscriberForManyCars()));
		lblRegularSubscriberFor.setBounds(78, 124, 287, 14);
		contentPane.add(lblRegularSubscriberFor);
		
		JLabel lblYourChanging = new JLabel("Your changing:");
		lblYourChanging.setBounds(10, 149, 117, 14);
		contentPane.add(lblYourChanging);
		
		JLabel lblChangeOccasionalParking = new JLabel("Change Occasional Parking Place price from:");
		lblChangeOccasionalParking.setBounds(10, 185, 296, 14);
		contentPane.add(lblChangeOccasionalParking);
		
		OccasionalFrom = new JTextField();
		OccasionalFrom.setBounds(279, 182, 86, 20);
		contentPane.add(OccasionalFrom);
		OccasionalFrom.setColumns(10);
		OccasionalFrom.setText(Double.toString(myp.getOccasionalParkingPlace()));
		OccasionalFrom.setEditable(false); // unable to edit it.
		
		JLabel lblTo = new JLabel("to:");
		lblTo.setBounds(206, 210, 34, 14);
		contentPane.add(lblTo);
		
		OccasionalTo = new JTextField();
		OccasionalTo.setBounds(279, 207, 86, 20);
		contentPane.add(OccasionalTo);
		// this part will block any possibility for trying to type an id with "undigit" chars.
		OccasionalTo.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent key) {
				char c=key.getKeyChar();
				if(!(Character.isDigit(c))||c==KeyEvent.VK_BACK_SPACE|| c==KeyEvent.VK_DELETE||c==KeyEvent.VK_MINUS)
					if(c==KeyEvent.VK_PERIOD)return;
					else if(c==KeyEvent.VK_MINUS||!(Character.isDigit(c))){
						getToolkit().beep();
						key.consume();}
				}
		});
		OccasionalTo.setColumns(10);
		OccasionalTo.setText("0.0");
		
		JLabel lblChangeOrderedParking = new JLabel("Change Ordered Parking Place price from:");
		lblChangeOrderedParking.setBounds(10, 238, 264, 14);
		contentPane.add(lblChangeOrderedParking);
		
		OrderedFrom = new JTextField();
		OrderedFrom.setBounds(279, 235, 86, 20);
		contentPane.add(OrderedFrom);
		OrderedFrom.setColumns(10);
		OrderedFrom.setText(Double.toString(myp.getOrderedParkingPlace()));
		OrderedFrom.setEditable(false); // unable to edit it.
		
		JLabel lblTo_1 = new JLabel("to:");
		lblTo_1.setBounds(197, 263, 34, 14);
		contentPane.add(lblTo_1);
		
		OrderedTo = new JTextField();
		OrderedTo.setBounds(279, 263, 86, 20);
		contentPane.add(OrderedTo);
		
		// this part will block any possibility for trying to type an id with "undigit" chars.
		OrderedTo.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent key) {
				char c=key.getKeyChar();
				if(!(Character.isDigit(c))||c==KeyEvent.VK_BACK_SPACE|| c==KeyEvent.VK_DELETE||c==KeyEvent.VK_MINUS)
					if(c==KeyEvent.VK_PERIOD)return;
					else if(c==KeyEvent.VK_MINUS||!(Character.isDigit(c))){
						getToolkit().beep();
						key.consume();}
				}
		});
		OrderedTo.setColumns(10);
		OrderedTo.setText("0.0");
		
		JLabel lblDateOfRequest = new JLabel("Date of request: "+myp.getDateOfSubmission());
		lblDateOfRequest.setBounds(10, 288, 320, 14);
		contentPane.add(lblDateOfRequest);
		
		JButton btnNewButton = new JButton("Send to compamy manager");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				myGUIr.getChangePrices().setOccasionalParkingPlace(Double.parseDouble(OccasionalTo.getText()));// getting the occasional price from the user.
				myGUIr.getChangePrices().setOrderedParkingPlace(Double.parseDouble(OrderedTo.getText()));// getting the ordered price from the user.
				if(myGUIr.validate()==pvalidateErrs.OCCERR){ // if the user didn't entered occasional price, throw error pop up.
					JOptionPane errid=new JOptionPane();
					String erridmsg="You didn't enter any new occasional price \nPlease enter a new price.";
					errid.setMessage(erridmsg);
					errid.setMessageType(JOptionPane.ERROR_MESSAGE);
					JDialog iddidalog=errid.createDialog(null,"Error! No new occasional price.");
					iddidalog.setVisible(true);
				} 
				else if(myGUIr.validate()==pvalidateErrs.ORDERR){ // if the user didn't enter ordered price, throw  error pop up. 
					JOptionPane errid=new JOptionPane();
					String erridmsg="You didn't enter any new ordered price \nPlease enter a new price.";
					errid.setMessage(erridmsg);
					errid.setMessageType(JOptionPane.ERROR_MESSAGE);
					JDialog iddidalog=errid.createDialog(null,"Error! No new ordered price.");
					iddidalog.setVisible(true);
				} 
				else if(myGUIr.validate()==null){ // else, throw a good pop up.
					JOptionPane errid=new JOptionPane(); 
					String okidmsg="You finished entering your request. now sending...";
					errid.setMessage(okidmsg);
					errid.setMessageType(JOptionPane.INFORMATION_MESSAGE);
					JDialog iddidalog=errid.createDialog(null,"Sending...");
					iddidalog.setVisible(true);
					okflag=true;
				}
			}
		});
		
		// if everything went well, save to the DB.
		if(okflag)
			myGUIr.save();
		btnNewButton.setBounds(20, 320, 195, 23);
		contentPane.add(btnNewButton);
		
		JButton btnCancelgoBack = new JButton("Cancel/Go back");
		btnCancelgoBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				close();
				frame.setContentPane(lastPanel);
				frame.setVisible(true);
			}
		});
		btnCancelgoBack.setBounds(263, 320, 135, 23);
		contentPane.add(btnCancelgoBack);
		
		JLabel lblAfterSendingAlso = new JLabel("After sending also cick \"go back\"");
		lblAfterSendingAlso.setBounds(30, 346, 228, 14);
		contentPane.add(lblAfterSendingAlso);
		
		JLabel lblIfYouWant = new JLabel("if you want to cancel");
		lblIfYouWant.setBounds(268, 346, 130, 14);
		contentPane.add(lblIfYouWant);
	}

	public void close()
	{
		WindowEvent windowclose=new WindowEvent(this,WindowEvent.WINDOW_CLOSING);
		Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(windowclose);
	}
}
